global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_A13_alternative_weights.log, replace name(tabl)

capture noi {
	
	* v3
	qui do ${code_dir}/config/tabletools.do

	estimates clear

	foreach window in f1970t1989 10yr from1970 from1970_pauto95 from1970_GDP0 from1970_GDP1 from1970_totlsw {
		if "`window'" == "from1970" {
			local year 2000
		}
		else {
			local year 1995
		}
		if "`window'" == "from1970_pauto95" {
			use ${final_dir}/regression_dataset_`window'.dta, clear
		}
		else {
			use ${final_dir}/regression_dataset_`window'_tfacit1.dta, clear
		}

		******************************************************
		clonevar LSW = lswMPm_1995_a
		clonevar HSW = hswMPm_1995_a
		clonevar VAEMP = vaempMPm_1995_a
		clonevar GDPGAP = lngdpgap_1995_a
		clonevar GDPPC = gdppcMPm_1995_a
		clonevar GDPPC_foreign = gdppcMPm_shr4_foreign_1995_a
		clonevar VAEMP_foreign = vaempMPm_shr4_foreign_1995_a
		clonevar GDPgap_foreign = lngdpgap_shr_foreign_1995_a
		clonevar LSW_foreign = lswMPm_shr4_foreign_1995_a
		clonevar HSW_foreign = hswMPm_shr4_foreign_1995_a
		clonevar spilloversown = spill${depvar}${ttt}_1995_a
		clonevar spilloversother = spillN${depvar}${ttt}_1995_a
		clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
		clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0
		clonevar stockown = k${depvar}_${ttt} 
		clonevar stockownzero = k${depvar}_${ttt}0  
		clonevar stockother = kNOT_${depvar}_${ttt} 
		clonevar stockotherzero = kNOT_${depvar}_${ttt}0
		bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=`year'+2 & year <= 2009+2
		bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
		drop _total_${depvar}_${ttt}_1995
		egen yearctry = group(year country_shr_1995) if year <= 2009
		egen yearindustry = group(year industry) if year <= 2009
		egen yearctryindustry = group(year industry country_shr_1995) if year <= 2009
		sort lse_id year

		ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=`year' & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
		estadd local f "\yes"
		estadd local iy "\yes"
		estadd local cy "\yes"
		estadd local has_stockspill "\yes"
		estadd local obs "{\num{`e(N)'}}"
		estadd local firms "{\num{`e(N_clust)'}}"
		estimates store c`window'_fd
		
		ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=`year' & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
		estadd local f "\yes"
		estadd local iy "\yes"
		estadd local cy "\yes"
		estadd local has_stockspill "\yes"
		estadd local obs "{\num{`e(N)'}}"
		estadd local firms "{\num{`e(N_clust)'}}"
		estimates store c`window'_f

		* Save lsw var for corr
		mmerge BvD using ${final_dir}/bvd_list_regfirms_auto95.dta, unmatched(master)
		keep if _m == 3
		keep if year >= 1995 & year <= 2009
		keep year BvD LSW 
		ren LSW LSW_`window'
		tempfile lswfile_`window'
		save `lswfile_`window'', replace
	}

	*make table varying pre-determined weights

	setlabels
	dhoztab cfrom1970_pauto95_fd cf1970t1989_fd c10yr_fd cfrom1970_fd cfrom1970_pauto95_f cf1970t1989_f c10yr_f cfrom1970_f using ${tab_dir}/appendix/Table_A13_alternative_weights.tex, b(%10.2f) se(%10.2f) showdepvarinfo nogaps replace numbers nonotes nolines nomtitles sfmt(a1) scalars("has_stockspill \noalign{\medskip} Stocks and spillovers" "f Firm fixed effects" "iy Industry \stimes year fixed effects" "cy Country \stimes year fixed effects" "obs \noalign{\medskip} Observations"  "firms Number of firms") label rawaddheader( ///
	Weight robustness &\multicolumn{2}{c}{Pauto95}&\multicolumn{2}{c}{1971--1989}&\multicolumn{2}{c}{1985--1994}&\multicolumn{2}{c}{start 2000} /// 
	\\ \cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}\cmidrule(lr){8-9} ///
	& {\makecell[c]{Dom. \\ and fgn.}} & {Fgn.} &{\makecell[c]{Dom. \\ and fgn.}} & {Fgn.} &{\makecell[c]{Dom. \\ and fgn.}} & {Fgn.}&{\makecell[c]{Dom. \\ and fgn.}} & {Fgn.}\\) ///
	drop(*zero _cons spill* stock*) noobs rename(LSW_foreign LSW HSW_foreign HSW GDPgap_foreign GDPGAP VAEMP_foreign VAEMP GDPPC_foreign GDPPC) substitute(\_ _) ///
		depvar("Auto95") ///
		notes("This table uses alternative weights to compute firm's macroeconomic variables. In Columns 1--2 the firm's country weights are calculated using pauto95 patents (machinery patents excluding auto95). Columns 2--4 compute the weights over the period 1971--1989 and Columns 5--6 over the period 1985--1994. Columns 7--8 use the baseline pre-sample period of 1971--1994 to compute weights but restrict the regression sample to the years 2000--2009. In columns 2, 4, 6, and 8 the macroeconmic variables are the normalized foreign variables as described in the text. Standard errors are clustered at the firm-level and reported in parentheses.")
	
	
	
	* Report: LSW correlations - not a table but we report a number.
	cap log using ${numb_dir}/table_A13_alternative_weights_numbers.log, repalce name(numb)
	use BvD year lswMPm_1995_a using ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear 
	mmerge BvD using ${final_dir}/bvd_list_regfirms_auto95.dta, unmatched(master)
	keep if _m == 3
	keep if year >= 1995 & year <= 2009
	rename lswMPm_1995_a LSW
	foreach window in f1970t1989 10yr from1970 from1970_pauto95 from1970_GDP0 from1970_GDP1 from1970_totlsw{
		mmerge BvD year using `lswfile_`window'', unmatched(master)
	}
	drop _m
	ren LSW_* _*
	estpost corr LSW _f1970t1989 _10yr _from1970 _from1970_pauto95 _from1970_GDP0 _from1970_GDP1 _from1970_totlsw, matrix
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store corr
	cap log close numb
	
	dhoztab corr using ${tab_dir}/appendix/alternative_weights_lsw_corr_numbers.tex, unstack m(8) replace noobs nopvalues nomtitles ///
		nomtitles nonumbers depvar(none) label cells(b(fmt(%10.3f)))  ///
		notes("Correlation of Low-Skill wage across the different weighting periods and methods; based on the baseline regression firms and time period 1995-2009. (Not in paper).")

	dhoztab corr using ${numb_dir}/alternative_weights_lsw_corr_numbers.tex, unstack m(8) replace noobs nopvalues nomtitles ///
		nomtitles nonumbers depvar(none) label cells(b(fmt(%10.3f)))  ///
		notes("Correlation of Low-Skill wage across the different weighting periods and methods; based on the baseline regression firms and time period 1995-2009. (Not in paper).")

    *again, } brackets
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}